package com.thwj.project.app.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thwj.common.annotation.Log;
import com.thwj.common.enums.BusinessType;
import com.thwj.framework.web.domain.R;
import com.thwj.framework.web.service.TokenService;
import com.thwj.project.app.api.param.MerchantParam;
import com.thwj.project.app.api.param.MerchantSearchParam;
import com.thwj.project.app.api.vo.MerchantVO;
import com.thwj.project.app.service.AppMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags="app 商家相关接口")
@RestController
@RequestMapping("/app/merchant")
public class AppMerchantController {


    @Autowired
    private AppMerchantService appMerchantService;
    @Autowired
    private TokenService tokenService;



    @ApiOperation(value = "获取商家分页列表 分页只要传页码 大小后端控制")
    @GetMapping("/getMerchantPage")
    public R<Page<MerchantVO>> getMerchantPage(Page page, MerchantSearchParam searchParam) {
        Page<MerchantVO> activityPage = appMerchantService.getMerchantPage(page,searchParam);
        return R.data(activityPage);
    }


    @ApiOperation(value = "获取推荐商家列表根据分类或者指定一个商家")
    @GetMapping("/getRecMerchantListByCategory")
    public R<List<MerchantVO>> getRecMerchantListByCategory(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long merchantId,
            @RequestParam(required = false) Long productId
    ) {
        if (categoryId != null) {
            List<MerchantVO> merchantVOList = appMerchantService.getRecMerchantListByCategory(categoryId);
            return R.data(merchantVOList);
        }
        if (merchantId != null) {
            List<MerchantVO> merchantVOList = appMerchantService.getRecMerchantListByMerchantId(merchantId);
            return R.data(merchantVOList);
        }
        if (productId != null) {
            List<MerchantVO> merchantVOList = appMerchantService.getRecMerchantListByProductId(productId);
            return R.data(merchantVOList);
        }


        return R.data(null);
    }


    @ApiOperation(value = "获取当前用户申请商家信息",notes = "获取当前用户商家信息")
    @GetMapping("/getMerchantInfo")
    public R<MerchantVO> getMerchantInfo() {
        MerchantVO merchantVO = appMerchantService.getMerchantInfo(tokenService.getLoginUser());
        return R.data(merchantVO);
    }

    @ApiOperation(value = "商家详情页",notes = "商家详情页")
    @GetMapping("/getMerchantDetail")
    public R<MerchantVO> getMerchantDetail(@RequestParam Long merchantId) {
        MerchantVO merchantVO = appMerchantService.getMerchantDetail(merchantId);
        return R.data(merchantVO);
    }

    @Log(title = "商家提交申请信息", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "提交用户的申请的商家信息，全部需要后台审核然后才会写入正式的商家表",notes = "提交用户的申请的商家信息")
    @PostMapping("/commitMerchantApply")
    public R<String> commitMerchantApply(@RequestBody MerchantParam merchantParam) {
        appMerchantService.commitMerchantApply(merchantParam, tokenService.getLoginUser());
        return R.data("关注公众号接受审核结果通知");
    }

    @Log(title = "商家更新信息", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "直接更新用户信息，不需要审核 目前不使用",notes = "发起提现申请操作")
    @PostMapping("/updateMerchant")
    public R<String> updateMerchant(@RequestBody MerchantParam merchantParam) {
        appMerchantService.updateMerchant(merchantParam, tokenService.getLoginUser());
        return R.data("更新成功");
    }


}
